<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        <meta charset="UTF-8">
        <title>公告管理</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    
    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">演示</a>
                <a>
                    <cite>导航元素</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5">
                                <div class="layui-inline layui-show-xs-block">
                                    <input class="layui-input" autocomplete="off" placeholder="开始日" name="start" id="start"></div>
                                <div class="layui-inline layui-show-xs-block">
                                    <input class="layui-input" autocomplete="off" placeholder="截止日" name="end" id="end"></div>
<!--                                <div class="layui-inline layui-show-xs-block">
                                    <select name="city" lay-verify="">
                                        <option value="">请选择状态</option>
                                        <option value="010">已处理</option>
                                        <option value="021">未处理</option>
                                    </select></div>-->
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn" lay-submit="" lay-filter="sreach">
                                        <i class="layui-icon">&#xe615;</i></button>
                                </div>
                            </form>
                        </div>
                        <div class="layui-card-body ">
                            <table class="layui-table" lay-data="{url:'FindNoticeByPage',page:true,toolbar: '#toolbarDemo',id:'tables'}" lay-filter="test">
                                <thead>
                                    <tr>
                                        <th lay-data="{type:'checkbox'}"></th>
                                        <th lay-data="{field:'id',align:'center'}">编号</th>
                                        <th lay-data="{field:'title',align:'center'}">标题</th>
                                        <th lay-data="{field:'time', sort: true}">发布日期</th>
                                        <th lay-data="{field:'username',align:'center'}">发布人</th>
                                        <th lay-data="{fixed: '',  align:'center', toolbar: '#barDemo'}">操作</th>

                                </thead>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <script type="text/html" id="toolbarDemo">
        <div class = "layui-btn-container" >
            <button class = "layui-btn  layui-btn-sm" lay-event = "addUser" > 添加公告</button>
<!--            <button class = "layui-btn  layui-btn-sm" lay-event = "modify" > 修改用户</button>-->
            <button class = "layui-btn layui-btn-danger layui-btn-sm" lay-event = "delAll"> 批量删除</button>
        </div >
    </script>

    <script type="text/html" id="barStatus">
        <input type="checkbox" name="status" id="{{d.id}}" value="{{d.status}}" lay-skin="switch" lay-filter="status" lay-text="已处理|未处理" {{ d.status ==0 ? 'checked':''}}>
    </script>

    <script type="text/html" id="barDemo">
        <a title="查看"  lay-event="detail">
            <i class="layui-icon">&#xe63c;</i></a>
        <a title="删除" lay-event = "del">
            <i class="layui-icon" >&#xe640;</i></a>
    </script>

    <script type="text/html" id="switchTpl">
        <!-- 这里的checked的状态只是演示 -->
        <input type = "checkbox" name = "sex" value = "{{d.id}}" lay-skin = "switch" lay-text = "女|男" lay-filter = "sexDemo" {{ d.sex =='女' ? 'checked': ''}} >
    </script>
    <script>layui.use('laydate',
        function() {
            var laydate = layui.laydate;

            //执行一个laydate实例
            laydate.render({
                elem: '#start' //指定元素
            });

            //执行一个laydate实例
            laydate.render({
                elem: '#end' //指定元素
            });

        });</script>
    <script>layui.use('table',
        function() {
            var table = layui.table;
            var $ = layui.$;

            //监听行工具事件
            table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,layEvent = obj.event; //获得 lay-event 对应的值
                if(layEvent === 'detail'){
                    xadmin.open('查看','details.html?id='+data.id,400,300);
                } else if(layEvent === 'del'){
                    layer.confirm('真的删除行么', function(index){

                        $.get("DelComplaint?id="+data.id, function(result){
                            console.log(result)
                        });

                        console.log(data.id)
                        obj.del(); //删除对应行（tr）的DOM结构
                        layer.close(index);
                        //向服务端发送删除指令
                    });
                }else if (layEvent === 'status'){
                    console.log(data.id)

                }
            });

            //状态监听
            layui.use(['form'], function () {
                var form = layui.form;
                form.on('switch(status)',function (data) {
                    //开关是否开启，true或者false
                    var checked = data.elem.checked;
                    //获取所需属性值
                    var value = data.elem.attributes['value'].nodeValue;
                    var id = data.elem.attributes['id'].nodeValue;

                    console.log(checked);

                    $.get("UpComplaint?id="+id+"&status="+checked, function(result){
                        console.log(result);
                        window.location.reload();
                    });
                });
            });



            //监听单元格编辑
            table.on('edit(test)',
            function(obj) {
                var value = obj.value //得到修改后的值
                ,
                data = obj.data //得到所在行所有键值
                ,
                field = obj.field; //得到字段
                layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);
            });



            //头工具栏事件
            table.on('toolbar(test)',
            function(obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'modify':
                    var data = checkStatus.data;
                    if(data.length>1){
                        layer.msg('每次只能编辑一名用户');
                    } else if(data.length==0){
                        layer.msg('请选择一名用户编辑');
                    }else{
                        xadmin.open('编辑','editUser.html?id='+data[0]['id'],600,400)
                    }
                    break;
                case 'delAll':
                    var data = checkStatus.data;
                    var ids = [];
                    for (var i = 0;i<data.length;i++){
                        ids.push(data[i]['id']);

                    }

                    layer.confirm('确认要删除吗？',function(index){
                        //捉到所有被选中的，发异步进行删除
/*                        $.ajax({
                            type:"post",
                            url:"/delSelected",
                            data:{"ids":'['+ids.join(',')+']'},
                            dataType:'text',
                            success:function (msg) {
                                if(msg=="success"){
                                    layer.msg('删除成功', {icon: 1});
                                    $(".layui-form-checked").not('.header').parents('tr').remove();
                                }
                            }
                        });*/

                    });
                    break;
                };
            });
        });

    function delAll (obj) {
        var ids = [];


        // 获取选中的id
        $('tbody input').each(function(index, el) {


            if($(this).prop('checked')){
                ids.push($(this).val())
            }
        });

        layer.confirm('确认要删除吗？'+ids.toString(),function(index){
            //捉到所有被选中的，发异步进行删除
            layer.msg('删除成功', {icon: 1});
            $(".layui-form-checked").not('.header').parents('tr').remove();
        });
    }
    </script>

</html>